home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / bison-1.22 / bison.info-1 < prev    next >
Encoding:
Text File  |  1992-09-08  |  49.4 KB  |  1,112 lines  |  [TEXT/EMAC]

  1. Info file bison.info, produced by Makeinfo, -*- Text -*- from input
  2. file bison.texinfo.
  3.  
  4.    This file documents the Bison parser generator.
  5.  
  6.    Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of
  9. this manual provided the copyright notice and this permission notice
  10. are preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the sections entitled "GNU General Public License" and
  15. "Conditions for Using Bison" are included exactly as in the original,
  16. and provided that the entire resulting derived work is distributed
  17. under the terms of a permission notice identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the sections entitled "GNU General Public
  22. License", "Conditions for Using Bison" and this permission notice may
  23. be included in translations approved by the Free Software Foundation
  24. instead of in the original English.
  25.  
  26. 
  27. File: bison.info,  Node: Top,  Next: Introduction,  Prev: (DIR),  Up: (DIR)
  28.  
  29.    This manual documents version 1.16 of Bison.
  30.  
  31. * Menu:
  32.  
  33. * Introduction::
  34. * Conditions::
  35. * Copying::           The GNU General Public License says
  36.                         how you can copy and share Bison
  37.  
  38. Tutorial sections:
  39. * Concepts::          Basic concepts for understanding Bison.
  40. * Examples::          Three simple explained examples of using Bison.
  41.  
  42. Reference sections:
  43. * Grammar File::      Writing Bison declarations and rules.
  44. * Interface::         C-language interface to the parser function `yyparse'.
  45. * Algorithm::         How the Bison parser works at run-time.
  46. * Error Recovery::    Writing rules for error recovery.
  47. * Context Dependency::What to do if your language syntax is too
  48.                         messy for Bison to handle straightforwardly.
  49. * Debugging::         Debugging Bison parsers that parse wrong.
  50. * Invocation::        How to run Bison (to produce the parser source file).
  51. * Table of Symbols::  All the keywords of the Bison language are explained.
  52. * Glossary::          Basic concepts are explained.
  53. * Index::             Cross-references to the text.
  54.  
  55. 
  56. File: bison.info,  Node: Introduction,  Next: Conditions,  Prev: Top,  Up: Top
  57.  
  58. Introduction
  59. ************
  60.  
  61.    "Bison" is a general-purpose parser generator that converts a
  62. grammar description for an LALR(1) context-free grammar into a C
  63. program to parse that grammar.  Once you are proficient with Bison,
  64. you may use it to develop a wide range of language parsers, from those
  65. used in simple desk calculators to complex programming languages.
  66.  
  67.    Bison is upward compatible with Yacc: all properly-written Yacc
  68. grammars ought to work with Bison with no change.  Anyone familiar
  69. with Yacc should be able to use Bison with little trouble.  You need
  70. to be fluent in C programming in order to use Bison or to understand
  71. this manual.
  72.  
  73.    We begin with tutorial chapters that explain the basic concepts of
  74. using Bison and show three explained examples, each building on the
  75. last.  If you don't know Bison or Yacc, start by reading these
  76. chapters.  Reference chapters follow which describe specific aspects
  77. of Bison in detail.
  78.  
  79.    Bison was written primarily by Robert Corbett; Richard Stallman made
  80. it Yacc-compatible.  This edition corresponds to version 1.16 of Bison.
  81.  
  82. 
  83. File: bison.info,  Node: Conditions,  Next: Copying,  Prev: Introduction,  Up: Top
  84.  
  85. Conditions for Using Bison
  86. **************************
  87.  
  88.    Bison grammars can be used only in programs that are free software.
  89.  This is in contrast to what happens with the GNU C compiler and the
  90. other GNU programming tools.
  91.  
  92.    The reason Bison is special is that the output of the Bison
  93. utility--the Bison parser file--contains a verbatim copy of a sizable
  94. piece of Bison, which is the code for the `yyparse' function.  (The
  95. actions from your grammar are inserted into this function at one
  96. point, but the rest of the function is not changed.)
  97.  
  98.    As a result, the Bison parser file is covered by the same copying
  99. conditions that cover Bison itself and the rest of the GNU system: any
  100. program containing it has to be distributed under the standard GNU
  101. copying conditions.
  102.  
  103.    Occasionally people who would like to use Bison to develop
  104. proprietary programs complain about this.
  105.  
  106.    We don't particularly sympathize with their complaints.  The
  107. purpose of the GNU project is to promote the right to share software
  108. and the practice of sharing software; it is a means of changing
  109. society.  The people who complain are planning to be uncooperative
  110. toward the rest of the world; why should they deserve our help in
  111. doing so?
  112.  
  113.    However, it's possible that a change in these conditions might
  114. encourage computer companies to use and distribute the GNU system.  If
  115. so, then we might decide to change the terms on `yyparse' as a matter
  116. of the strategy of promoting the right to share.  Such a change would
  117. be irrevocable.  Since we stand by the copying permissions we have
  118. announced, we cannot withdraw them once given.
  119.  
  120.    We mustn't make an irrevocable change hastily.  We have to wait
  121. until there is a complete GNU system and there has been time to learn
  122. how this issue affects its reception.
  123.  
  124. 
  125. File: bison.info,  Node: Copying,  Next: Concepts,  Prev: Conditions,  Up: Top
  126.  
  127. GNU GENERAL PUBLIC LICENSE
  128. **************************
  129.  
  130.                              Version 2, June 1991
  131.  
  132.      Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  133.      675 Mass Ave, Cambridge, MA 02139, USA
  134.      
  135.      Everyone is permitted to copy and distribute verbatim copies
  136.      of this license document, but changing it is not allowed.
  137.  
  138. Preamble
  139. ========
  140.  
  141.    The licenses for most software are designed to take away your
  142. freedom to share and change it.  By contrast, the GNU General Public
  143. License is intended to guarantee your freedom to share and change free
  144. software--to make sure the software is free for all its users.  This
  145. General Public License applies to most of the Free Software
  146. Foundation's software and to any other program whose authors commit to
  147. using it.  (Some other Free Software Foundation software is covered by
  148. the GNU Library General Public License instead.)  You can apply it to
  149. your programs, too.
  150.  
  151.    When we speak of free software, we are referring to freedom, not
  152. price.  Our General Public Licenses are designed to make sure that you
  153. have the freedom to distribute copies of free software (and charge for
  154. this service if you wish), that you receive source code or can get it
  155. if you want it, that you can change the software or use pieces of it
  156. in new free programs; and that you know you can do these things.
  157.  
  158.    To protect your rights, we need to make restrictions that forbid
  159. anyone to deny you these rights or to ask you to surrender the rights. 
  160. These restrictions translate to certain responsibilities for you if you
  161. distribute copies of the software, or if you modify it.
  162.  
  163.    For example, if you distribute copies of such a program, whether
  164. gratis or for a fee, you must give the recipients all the rights that
  165. you have.  You must make sure that they, too, receive or can get the
  166. source code.  And you must show them these terms so they know their
  167. rights.
  168.  
  169.    We protect your rights with two steps: (1) copyright the software,
  170. and (2) offer you this license which gives you legal permission to
  171. copy, distribute and/or modify the software.
  172.  
  173.    Also, for each author's protection and ours, we want to make certain
  174. that everyone understands that there is no warranty for this free
  175. software.  If the software is modified by someone else and passed on,
  176. we want its recipients to know that what they have is not the
  177. original, so that any problems introduced by others will not reflect
  178. on the original authors' reputations.
  179.  
  180.    Finally, any free program is threatened constantly by software
  181. patents.  We wish to avoid the danger that redistributors of a free
  182. program will individually obtain patent licenses, in effect making the
  183. program proprietary.  To prevent this, we have made it clear that any
  184. patent must be licensed for everyone's free use or not licensed at all.
  185.  
  186.    The precise terms and conditions for copying, distribution and
  187. modification follow.
  188.  
  189.        TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  190.  
  191.   1. This License applies to any program or other work which contains
  192.      a notice placed by the copyright holder saying it may be
  193.      distributed under the terms of this General Public License.  The
  194.      "Program", below, refers to any such program or work, and a "work
  195.      based on the Program" means either the Program or any derivative
  196.      work under copyright law: that is to say, a work containing the
  197.      Program or a portion of it, either verbatim or with modifications
  198.      and/or translated into another language.  (Hereinafter,
  199.      translation is included without limitation in the term
  200.      "modification".)  Each licensee is addressed as "you".
  201.  
  202.         Activities other than copying, distribution and modification
  203.      are not covered by this License; they are outside its scope.  The
  204.      act of running the Program is not restricted, and the output from
  205.      the Program is covered only if its contents constitute a work
  206.      based on the Program (independent of having been made by running
  207.      the Program).  Whether that is true depends on what the Program
  208.      does.
  209.  
  210.   2. You may copy and distribute verbatim copies of the Program's
  211.      source code as you receive it, in any medium, provided that you
  212.      conspicuously and appropriately publish on each copy an
  213.      appropriate copyright notice and disclaimer of warranty; keep
  214.      intact all the notices that refer to this License and to the
  215.      absence of any warranty; and give any other recipients of the
  216.      Program a copy of this License along with the Program.
  217.  
  218.         You may charge a fee for the physical act of transferring a
  219.      copy, and you may at your option offer warranty protection in
  220.      exchange for a fee.
  221.  
  222.   3. You may modify your copy or copies of the Program or any portion
  223.      of it, thus forming a work based on the Program, and copy and
  224.      distribute such modifications or work under the terms of Section 1
  225.      above, provided that you also meet all of these conditions:
  226.  
  227.        1. You must cause the modified files to carry prominent notices
  228.           stating that you changed the files and the date of any
  229.           change.
  230.  
  231.        2. You must cause any work that you distribute or publish, that
  232.           in whole or in part contains or is derived from the Program
  233.           or any part thereof, to be licensed as a whole at no charge
  234.           to all third parties under the terms of this License.
  235.  
  236.        3. If the modified program normally reads commands interactively
  237.           when run, you must cause it, when started running for such
  238.           interactive use in the most ordinary way, to print or
  239.           display an announcement including an appropriate copyright
  240.           notice and a notice that there is no warranty (or else,
  241.           saying that you provide a warranty) and that users may
  242.           redistribute the program under these conditions, and telling
  243.           the user how to view a copy of this License.  (Exception: if
  244.           the Program itself is interactive but does not normally
  245.           print such an announcement, your work based on the Program
  246.           is not required to print an announcement.)
  247.  
  248.              These requirements apply to the modified work as a whole.  If
  249.      identifiable sections of that work are not derived from the
  250.      Program, and can be reasonably considered independent and
  251.      separate works in themselves, then this License, and its terms,
  252.      do not apply to those sections when you distribute them as
  253.      separate works.  But when you distribute the same sections as
  254.      part of a whole which is a work based on the Program, the
  255.      distribution of the whole must be on the terms of this License,
  256.      whose permissions for other licensees extend to the entire whole,
  257.      and thus to each and every part regardless of who wrote it.
  258.  
  259.         Thus, it is not the intent of this section to claim rights or
  260.      contest your rights to work written entirely by you; rather, the
  261.      intent is to exercise the right to control the distribution of
  262.      derivative or collective works based on the Program.
  263.  
  264.         In addition, mere aggregation of another work not based on the
  265.      Program with the Program (or with a work based on the Program) on
  266.      a volume of a storage or distribution medium does not bring the
  267.      other work under the scope of this License.
  268.  
  269.   4. You may copy and distribute the Program (or a work based on it,
  270.      under Section 2) in object code or executable form under the
  271.      terms of Sections 1 and 2 above provided that you also do one of
  272.      the following:
  273.  
  274.        1. Accompany it with the complete corresponding machine-readable
  275.           source code, which must be distributed under the terms of
  276.           Sections 1 and 2 above on a medium customarily used for
  277.           software interchange; or,
  278.  
  279.        2. Accompany it with a written offer, valid for at least three
  280.           years, to give any third party, for a charge no more than
  281.           your cost of physically performing source distribution, a
  282.           complete machine-readable copy of the corresponding source
  283.           code, to be distributed under the terms of Sections 1 and 2
  284.           above on a medium customarily used for software interchange;
  285.           or,
  286.  
  287.        3. Accompany it with the information you received as to the
  288.           offer to distribute corresponding source code.  (This
  289.           alternative is allowed only for noncommercial distribution
  290.           and only if you received the program in object code or
  291.           executable form with such an offer, in accord with
  292.           Subsection b above.)
  293.  
  294.              The source code for a work means the preferred form of the
  295.      work for making modifications to it.  For an executable work,
  296.      complete source code means all the source code for all modules it
  297.      contains, plus any associated interface definition files, plus
  298.      the scripts used to control compilation and installation of the
  299.      executable.  However, as a special exception, the source code
  300.      distributed need not include anything that is normally
  301.      distributed (in either source or binary form) with the major
  302.      components (compiler, kernel, and so on) of the operating system
  303.      on which the executable runs, unless that component itself
  304.      accompanies the executable.
  305.  
  306.         If distribution of executable or object code is made by
  307.      offering access to copy from a designated place, then offering
  308.      equivalent access to copy the source code from the same place
  309.      counts as distribution of the source code, even though third
  310.      parties are not compelled to copy the source along with the
  311.      object code.
  312.  
  313.   5. You may not copy, modify, sublicense, or distribute the Program
  314.      except as expressly provided under this License.  Any attempt
  315.      otherwise to copy, modify, sublicense or distribute the Program is
  316.      void, and will automatically terminate your rights under this
  317.      License.  However, parties who have received copies, or rights,
  318.      from you under this License will not have their licenses
  319.      terminated so long as such parties remain in full compliance.
  320.  
  321.   6. You are not required to accept this License, since you have not
  322.      signed it.  However, nothing else grants you permission to modify
  323.      or distribute the Program or its derivative works.  These actions
  324.      are prohibited by law if you do not accept this License. 
  325.      Therefore, by modifying or distributing the Program (or any work
  326.      based on the Program), you indicate your acceptance of this
  327.      License to do so, and all its terms and conditions for copying,
  328.      distributing or modifying the Program or works based on it.
  329.  
  330.   7. Each time you redistribute the Program (or any work based on the
  331.      Program), the recipient automatically receives a license from the
  332.      original licensor to copy, distribute or modify the Program
  333.      subject to these terms and conditions.  You may not impose any
  334.      further restrictions on the recipients' exercise of the rights
  335.      granted herein.  You are not responsible for enforcing compliance
  336.      by third parties to this License.
  337.  
  338.   8. If, as a consequence of a court judgment or allegation of patent
  339.      infringement or for any other reason (not limited to patent
  340.      issues), conditions are imposed on you (whether by court order,
  341.      agreement or otherwise) that contradict the conditions of this
  342.      License, they do not excuse you from the conditions of this
  343.      License.  If you cannot distribute so as to satisfy
  344.      simultaneously your obligations under this License and any other
  345.      pertinent obligations, then as a consequence you may not
  346.      distribute the Program at all.  For example, if a patent license
  347.      would not permit royalty-free redistribution of the Program by
  348.      all those who receive copies directly or indirectly through you,
  349.      then the only way you could satisfy both it and this License
  350.      would be to refrain entirely from distribution of the Program.
  351.  
  352.         If any portion of this section is held invalid or
  353.      unenforceable under any particular circumstance, the balance of
  354.      the section is intended to apply and the section as a whole is
  355.      intended to apply in other circumstances.
  356.  
  357.         It is not the purpose of this section to induce you to
  358.      infringe any patents or other property right claims or to contest
  359.      validity of any such claims; this section has the sole purpose of
  360.      protecting the integrity of the free software distribution
  361.      system, which is implemented by public license practices.  Many
  362.      people have made generous contributions to the wide range of
  363.      software distributed through that system in reliance on
  364.      consistent application of that system; it is up to the
  365.      author/donor to decide if he or she is willing to distribute
  366.      software through any other system and a licensee cannot impose
  367.      that choice.
  368.  
  369.         This section is intended to make thoroughly clear what is
  370.      believed to be a consequence of the rest of this License.
  371.  
  372.   9. If the distribution and/or use of the Program is restricted in
  373.      certain countries either by patents or by copyrighted interfaces,
  374.      the original copyright holder who places the Program under this
  375.      License may add an explicit geographical distribution limitation
  376.      excluding those countries, so that distribution is permitted only
  377.      in or among countries not thus excluded.  In such case, this
  378.      License incorporates the limitation as if written in the body of
  379.      this License.
  380.  
  381.  10. The Free Software Foundation may publish revised and/or new
  382.      versions of the General Public License from time to time.  Such
  383.      new versions will be similar in spirit to the present version,
  384.      but may differ in detail to address new problems or concerns.
  385.  
  386.         Each version is given a distinguishing version number.  If the
  387.      Program specifies a version number of this License which applies
  388.      to it and "any later version", you have the option of following
  389.      the terms and conditions either of that version or of any later
  390.      version published by the Free Software Foundation.  If the
  391.      Program does not specify a version number of this License, you
  392.      may choose any version ever published by the Free Software
  393.      Foundation.
  394.  
  395.  11. If you wish to incorporate parts of the Program into other free
  396.      programs whose distribution conditions are different, write to
  397.      the author to ask for permission.  For software which is
  398.      copyrighted by the Free Software Foundation, write to the Free
  399.      Software Foundation; we sometimes make exceptions for this.  Our
  400.      decision will be guided by the two goals of preserving the free
  401.      status of all derivatives of our free software and of promoting
  402.      the sharing and reuse of software generally.
  403.  
  404.                                       NO WARRANTY
  405.  
  406.  12. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  407.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  408.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  409.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
  410.      WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
  411.      NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  412.      FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE
  413.      QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  414.      PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  415.      SERVICING, REPAIR OR CORRECTION.
  416.  
  417.  13. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  418.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  419.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  420.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  421.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  422.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS
  423.      OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  424.      YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH
  425.      ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  426.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  427.  
  428.                          END OF TERMS AND CONDITIONS
  429.  
  430. How to Apply These Terms to Your New Programs
  431. =============================================
  432.  
  433.    If you develop a new program, and you want it to be of the greatest
  434. possible use to the public, the best way to achieve this is to make it
  435. free software which everyone can redistribute and change under these
  436. terms.
  437.  
  438.    To do so, attach the following notices to the program.  It is safest
  439. to attach them to the start of each source file to most effectively
  440. convey the exclusion of warranty; and each file should have at least
  441. the "copyright" line and a pointer to where the full notice is found.
  442.  
  443.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  444.      Copyright (C) 19YY  NAME OF AUTHOR
  445.      
  446.      This program is free software; you can redistribute it and/or modify
  447.      it under the terms of the GNU General Public License as published by
  448.      the Free Software Foundation; either version 2 of the License, or
  449.      (at your option) any later version.
  450.      
  451.      This program is distributed in the hope that it will be useful,
  452.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  453.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  454.      GNU General Public License for more details.
  455.      
  456.      You should have received a copy of the GNU General Public License
  457.      along with this program; if not, write to the Free Software
  458.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  459.  
  460.    Also add information on how to contact you by electronic and paper
  461. mail.
  462.  
  463.    If the program is interactive, make it output a short notice like
  464. this when it starts in an interactive mode:
  465.  
  466.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  467.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  468.      This is free software, and you are welcome to redistribute it
  469.      under certain conditions; type `show c' for details.
  470.  
  471.    The hypothetical commands `show w' and `show c' should show the
  472. appropriate parts of the General Public License.  Of course, the
  473. commands you use may be called something other than `show w' and `show
  474. c'; they could even be mouse-clicks or menu items--whatever suits your
  475. program.
  476.  
  477.    You should also get your employer (if you work as a programmer) or
  478. your school, if any, to sign a "copyright disclaimer" for the program,
  479. if necessary.  Here is a sample; alter the names:
  480.  
  481.      Yoyodyne, Inc., hereby disclaims all copyright
  482.      interest in the program `Gnomovision'
  483.      (which makes passes at compilers) written
  484.      by James Hacker.
  485.      
  486.      SIGNATURE OF TY COON, 1 April 1989
  487.      Ty Coon, President of Vice
  488.  
  489.    This General Public License does not permit incorporating your
  490. program into proprietary programs.  If your program is a subroutine
  491. library, you may consider it more useful to permit linking proprietary
  492. applications with the library.  If this is what you want to do, use
  493. the GNU Library General Public License instead of this License.
  494.  
  495. 
  496. File: bison.info,  Node: Concepts,  Next: Examples,  Prev: Copying,  Up: Top
  497.  
  498. The Concepts of Bison
  499. *********************
  500.  
  501.    This chapter introduces many of the basic concepts without which the
  502. details of Bison will not make sense.  If you do not already know how
  503. to use Bison or Yacc, we suggest you start by reading this chapter
  504. carefully.
  505.  
  506. * Menu:
  507.  
  508. * Language and Grammar::  Languages and context-free grammars,
  509.                             as mathematical ideas.
  510. * Grammar in Bison::      How we represent grammars for Bison's sake.
  511. * Semantic Values::       Each token or syntactic grouping can have
  512.                             a semantic value (the value of an integer,
  513.                             the name of an identifier, etc.).
  514. * Semantic Actions::      Each rule can have an action containing C code.
  515. * Bison Parser::          What are Bison's input and output,
  516.                             how is the output used?
  517. * Stages::                Stages in writing and running Bison grammars.
  518. * Grammar Layout::        Overall structure of a Bison grammar file.
  519.  
  520. 
  521. File: bison.info,  Node: Language and Grammar,  Next: Grammar in Bison,  Prev: Concepts,  Up: Concepts
  522.  
  523. Languages and Context-Free Grammars
  524. ===================================
  525.  
  526.    In order for Bison to parse a language, it must be described by a
  527. "context-free grammar".  This means that you specify one or more
  528. "syntactic groupings" and give rules for constructing them from their
  529. parts.  For example, in the C language, one kind of grouping is called
  530. an `expression'.  One rule for making an expression might be, "An
  531. expression can be made of a minus sign and another expression". 
  532. Another would be, "An expression can be an integer".  As you can see,
  533. rules are often recursive, but there must be at least one rule which
  534. leads out of the recursion.
  535.  
  536.    The most common formal system for presenting such rules for humans
  537. to read is "Backus-Naur Form" or "BNF", which was developed in order to
  538. specify the language Algol 60.  Any grammar expressed in BNF is a
  539. context-free grammar.  The input to Bison is essentially
  540. machine-readable BNF.
  541.  
  542.    Not all context-free languages can be handled by Bison, only those
  543. that are LALR(1).  In brief, this means that it must be possible to
  544. tell how to parse any portion of an input string with just a single
  545. token of look-ahead.  Strictly speaking, that is a description of an
  546. LR(1) grammar, and LALR(1) involves additional restrictions that are
  547. hard to explain simply; but it is rare in actual practice to find an
  548. LR(1) grammar that fails to be LALR(1).  *Note  Mysterious
  549. Reduce/Reduce Conflicts: Mystery Conflicts, for more information on
  550. this.
  551.  
  552.    In the formal grammatical rules for a language, each kind of
  553. syntactic unit or grouping is named by a "symbol".  Those which are
  554. built by grouping smaller constructs according to grammatical rules
  555. are called "nonterminal symbols"; those which can't be subdivided are
  556. called "terminal symbols" or "token types".  We call a piece of input
  557. corresponding to a single terminal symbol a "token", and a piece
  558. corresponding to a single nonterminal symbol a "grouping".
  559.  
  560.    We can use the C language as an example of what symbols, terminal
  561. and nonterminal, mean.  The tokens of C are identifiers, constants
  562. (numeric and string), and the various keywords, arithmetic operators
  563. and punctuation marks.  So the terminal symbols of a grammar for C
  564. include `identifier', `number', `string', plus one symbol for each
  565. keyword, operator or punctuation mark: `if', `return', `const',
  566. `static', `int', `char', `plus-sign', `open-brace', `close-brace',
  567. `comma' and many more.  (These tokens can be subdivided into
  568. characters, but that is a matter of lexicography, not grammar.)
  569.  
  570.    Here is a simple C function subdivided into tokens:
  571.  
  572.      int             /* keyword `int' */
  573.      square (x)      /* identifier, open-paren, */
  574.                      /* identifier, close-paren */
  575.           int x;     /* keyword `int', identifier, semicolon */
  576.      {               /* open-brace */
  577.        return x * x; /* keyword `return', identifier, */
  578.                      /* asterisk, identifier, semicolon */
  579.      }               /* close-brace */
  580.  
  581.    The syntactic groupings of C include the expression, the statement,
  582. the declaration, and the function definition.  These are represented
  583. in the grammar of C by nonterminal symbols `expression', `statement',
  584. `declaration' and `function definition'.  The full grammar uses dozens
  585. of additional language constructs, each with its own nonterminal
  586. symbol, in order to express the meanings of these four.  The example
  587. above is a function definition; it contains one declaration, and one
  588. statement.  In the statement, each `x' is an expression and so is `x *
  589. x'.
  590.  
  591.    Each nonterminal symbol must have grammatical rules showing how it
  592. is made out of simpler constructs.  For example, one kind of C
  593. statement is the `return' statement; this would be described with a
  594. grammar rule which reads informally as follows:
  595.  
  596.      A `statement' can be made of a `return' keyword, an `expression'
  597.      and a `semicolon'.
  598.  
  599. There would be many other rules for `statement', one for each kind of
  600. statement in C.
  601.  
  602.    One nonterminal symbol must be distinguished as the special one
  603. which defines a complete utterance in the language.  It is called the
  604. "start symbol".  In a compiler, this means a complete input program. 
  605. In the C language, the nonterminal symbol `sequence of definitions and
  606. declarations' plays this role.
  607.  
  608.    For example, `1 + 2' is a valid C expression--a valid part of a C
  609. program--but it is not valid as an *entire* C program.  In the
  610. context-free grammar of C, this follows from the fact that
  611. `expression' is not the start symbol.
  612.  
  613.    The Bison parser reads a sequence of tokens as its input, and
  614. groups the tokens using the grammar rules.  If the input is valid, the
  615. end result is that the entire token sequence reduces to a single
  616. grouping whose symbol is the grammar's start symbol.  If we use a
  617. grammar for C, the entire input must be a `sequence of definitions and
  618. declarations'.  If not, the parser reports a syntax error.
  619.  
  620. 
  621. File: bison.info,  Node: Grammar in Bison,  Next: Semantic Values,  Prev: Language and Grammar,  Up: Concepts
  622.  
  623. From Formal Rules to Bison Input
  624. ================================
  625.  
  626.    A formal grammar is a mathematical construct.  To define the
  627. language for Bison, you must write a file expressing the grammar in
  628. Bison syntax: a "Bison grammar" file.  *Note Grammar File::.
  629.  
  630.    A nonterminal symbol in the formal grammar is represented in Bison
  631. input as an identifier, like an identifier in C.  By convention, it
  632. should be in lower case, such as `expr', `stmt' or `declaration'.
  633.  
  634.    The Bison representation for a terminal symbol is also called a
  635. "token type".  Token types as well can be represented as C-like
  636. identifiers.  By convention, these identifiers should be upper case to
  637. distinguish them from nonterminals: for example, `INTEGER',
  638. `IDENTIFIER', `IF' or `RETURN'.  A terminal symbol that stands for a
  639. particular keyword in the language should be named after that keyword
  640. converted to upper case.  The terminal symbol `error' is reserved for
  641. error recovery.  *Note Symbols::.
  642.  
  643.    A terminal symbol can also be represented as a character literal,
  644. just like a C character constant.  You should do this whenever a token
  645. is just a single character (parenthesis, plus-sign, etc.): use that
  646. same character in a literal as the terminal symbol for that token.
  647.  
  648.    The grammar rules also have an expression in Bison syntax.  For
  649. example, here is the Bison rule for a C `return' statement.  The
  650. semicolon in quotes is a literal character token, representing part of
  651. the C syntax for the statement; the naked semicolon, and the colon,
  652. are Bison punctuation used in every rule.
  653.  
  654.      stmt:   RETURN expr ';'
  655.              ;
  656.  
  657. *Note Rules::.
  658.  
  659. 
  660. File: bison.info,  Node: Semantic Values,  Next: Semantic Actions,  Prev: Grammar in Bison,  Up: Concepts
  661.  
  662. Semantic Values
  663. ===============
  664.  
  665.    A formal grammar selects tokens only by their classifications: for
  666. example, if a rule mentions the terminal symbol `integer constant', it
  667. means that *any* integer constant is grammatically valid in that
  668. position.  The precise value of the constant is irrelevant to how to
  669. parse the input: if `x+4' is grammatical then `x+1' or `x+3989' is
  670. equally grammatical.
  671.  
  672.    But the precise value is very important for what the input means
  673. once it is parsed.  A compiler is useless if it fails to distinguish
  674. between 4, 1 and 3989 as constants in the program!  Therefore, each
  675. token in a Bison grammar has both a token type and a "semantic value".
  676.  *Note Semantics::, for details.
  677.  
  678.    The token type is a terminal symbol defined in the grammar, such as
  679. `INTEGER', `IDENTIFIER' or `',''.  It tells everything you need to
  680. know to decide where the token may validly appear and how to group it
  681. with other tokens.  The grammar rules know nothing about tokens except
  682. their types.
  683.  
  684.    The semantic value has all the rest of the information about the
  685. meaning of the token, such as the value of an integer, or the name of
  686. an identifier.  (A token such as `','' which is just punctuation
  687. doesn't need to have any semantic value.)
  688.  
  689.    For example, an input token might be classified as token type
  690. `INTEGER' and have the semantic value 4.  Another input token might
  691. have the same token type `INTEGER' but value 3989.  When a grammar
  692. rule says that `INTEGER' is allowed, either of these tokens is
  693. acceptable because each is an `INTEGER'.  When the parser accepts the
  694. token, it keeps track of the token's semantic value.
  695.  
  696.    Each grouping can also have a semantic value as well as its
  697. nonterminal symbol.  For example, in a calculator, an expression
  698. typically has a semantic value that is a number.  In a compiler for a
  699. programming language, an expression typically has a semantic value
  700. that is a tree structure describing the meaning of the expression.
  701.  
  702. 
  703. File: bison.info,  Node: Semantic Actions,  Next: Bison Parser,  Prev: Semantic Values,  Up: Concepts
  704.  
  705. Semantic Actions
  706. ================
  707.  
  708.    In order to be useful, a program must do more than parse input; it
  709. must also produce some output based on the input.  In a Bison grammar,
  710. a grammar rule can have an "action" made up of C statements.  Each
  711. time the parser recognizes a match for that rule, the action is
  712. executed.  *Note Actions::.
  713.  
  714.    Most of the time, the purpose of an action is to compute the
  715. semantic value of the whole construct from the semantic values of its
  716. parts.  For example, suppose we have a rule which says an expression
  717. can be the sum of two expressions.  When the parser recognizes such a
  718. sum, each of the subexpressions has a semantic value which describes
  719. how it was built up.  The action for this rule should create a similar
  720. sort of value for the newly recognized larger expression.
  721.  
  722.    For example, here is a rule that says an expression can be the sum
  723. of two subexpressions:
  724.  
  725.      expr: expr '+' expr   { $$ = $1 + $3; }
  726.              ;
  727.  
  728. The action says how to produce the semantic value of the sum expression
  729. from the values of the two subexpressions.
  730.  
  731. 
  732. File: bison.info,  Node: Bison Parser,  Next: Stages,  Prev: Semantic Actions,  Up: Concepts
  733.  
  734. Bison Output: the Parser File
  735. =============================
  736.  
  737.    When you run Bison, you give it a Bison grammar file as input.  The
  738. output is a C source file that parses the language described by the
  739. grammar.  This file is called a "Bison parser".  Keep in mind that the
  740. Bison utility and the Bison parser are two distinct programs: the
  741. Bison utility is a program whose output is the Bison parser that
  742. becomes part of your program.
  743.  
  744.    The job of the Bison parser is to group tokens into groupings
  745. according to the grammar rules--for example, to build identifiers and
  746. operators into expressions.  As it does this, it runs the actions for
  747. the grammar rules it uses.
  748.  
  749.    The tokens come from a function called the "lexical analyzer" that
  750. you must supply in some fashion (such as by writing it in C).  The
  751. Bison parser calls the lexical analyzer each time it wants a new
  752. token.  It doesn't know what is "inside" the tokens (though their
  753. semantic values may reflect this).  Typically the lexical analyzer
  754. makes the tokens by parsing characters of text, but Bison does not
  755. depend on this.  *Note Lexical::.
  756.  
  757.    The Bison parser file is C code which defines a function named
  758. `yyparse' which implements that grammar.  This function does not make
  759. a complete C program: you must supply some additional functions.  One
  760. is the lexical analyzer.  Another is an error-reporting function which
  761. the parser calls to report an error.  In addition, a complete C
  762. program must start with a function called `main'; you have to provide
  763. this, and arrange for it to call `yyparse' or the parser will never
  764. run.  *Note Interface::.
  765.  
  766.    Aside from the token type names and the symbols in the actions you
  767. write, all variable and function names used in the Bison parser file
  768. begin with `yy' or `YY'.  This includes interface functions such as
  769. the lexical analyzer function `yylex', the error reporting function
  770. `yyerror' and the parser function `yyparse' itself.  This also
  771. includes numerous identifiers used for internal purposes.  Therefore,
  772. you should avoid using C identifiers starting with `yy' or `YY' in the
  773. Bison grammar file except for the ones defined in this manual.
  774.  
  775. 
  776. File: bison.info,  Node: Stages,  Next: Grammar Layout,  Prev: Bison Parser,  Up: Concepts
  777.  
  778. Stages in Using Bison
  779. =====================
  780.  
  781.    The actual language-design process using Bison, from grammar
  782. specification to a working compiler or interpreter, has these parts:
  783.  
  784.   1. Formally specify the grammar in a form recognized by Bison (*note
  785.      Grammar File::.).  For each grammatical rule in the language,
  786.      describe the action that is to be taken when an instance of that
  787.      rule is recognized.  The action is described by a sequence of C
  788.      statements.
  789.  
  790.   2. Write a lexical analyzer to process input and pass tokens to the
  791.      parser.  The lexical analyzer may be written by hand in C (*note
  792.      Lexical::.).  It could also be produced using Lex, but the use of
  793.      Lex is not discussed in this manual.
  794.  
  795.   3. Write a controlling function that calls the Bison-produced parser.
  796.  
  797.   4. Write error-reporting routines.
  798.  
  799.    To turn this source code as written into a runnable program, you
  800. must follow these steps:
  801.  
  802.   1. Run Bison on the grammar to produce the parser.
  803.  
  804.   2. Compile the code output by Bison, as well as any other source
  805.      files.
  806.  
  807.   3. Link the object files to produce the finished product.
  808.  
  809. 
  810. File: bison.info,  Node: Grammar Layout,  Prev: Stages,  Up: Concepts
  811.  
  812. The Overall Layout of a Bison Grammar
  813. =====================================
  814.  
  815.    The input file for the Bison utility is a "Bison grammar file".  The
  816. general form of a Bison grammar file is as follows:
  817.  
  818.      %{
  819.      C DECLARATIONS
  820.      %}
  821.      
  822.      BISON DECLARATIONS
  823.      
  824.      %%
  825.      GRAMMAR RULES
  826.      %%
  827.      ADDITIONAL C CODE
  828.  
  829. The `%%', `%{' and `%}' are punctuation that appears in every Bison
  830. grammar file to separate the sections.
  831.  
  832.    The C declarations may define types and variables used in the
  833. actions.  You can also use preprocessor commands to define macros used
  834. there, and use `#include' to include header files that do any of these
  835. things.
  836.  
  837.    The Bison declarations declare the names of the terminal and
  838. nonterminal symbols, and may also describe operator precedence and the
  839. data types of semantic values of various symbols.
  840.  
  841.    The grammar rules define how to construct each nonterminal symbol
  842. from its parts.
  843.  
  844.    The additional C code can contain any C code you want to use. 
  845. Often the definition of the lexical analyzer `yylex' goes here, plus
  846. subroutines called by the actions in the grammar rules.  In a simple
  847. program, all the rest of the program can go here.
  848.  
  849. 
  850. File: bison.info,  Node: Examples,  Next: Grammar File,  Prev: Concepts,  Up: Top
  851.  
  852. Examples
  853. ********
  854.  
  855.    Now we show and explain three sample programs written using Bison: a
  856. reverse polish notation calculator, an algebraic (infix) notation
  857. calculator, and a multi-function calculator.  All three have been
  858. tested under BSD Unix 4.3; each produces a usable, though limited,
  859. interactive desk-top calculator.
  860.  
  861.    These examples are simple, but Bison grammars for real programming
  862. languages are written the same way.
  863.  
  864.    You can copy these examples out of the Info file and into a source
  865. file to try them.
  866.  
  867. * Menu:
  868.  
  869. * RPN Calc::               Reverse polish notation calculator;
  870.                              a first example with no operator precedence.
  871. * Infix Calc::             Infix (algebraic) notation calculator.
  872.                              Operator precedence is introduced.
  873. * Simple Error Recovery::  Continuing after syntax errors.
  874. * Multi-function Calc::    Calculator with memory and trig functions.
  875.                              It uses multiple data-types for semantic values.
  876. * Exercises::              Ideas for improving the multi-function calculator.
  877.  
  878. 
  879. File: bison.info,  Node: RPN Calc,  Next: Infix Calc,  Prev: Examples,  Up: Examples
  880.  
  881. Reverse Polish Notation Calculator
  882. ==================================
  883.  
  884.    The first example is that of a simple double-precision "reverse
  885. polish notation" calculator (a calculator using postfix operators). 
  886. This example provides a good starting point, since operator precedence
  887. is not an issue.  The second example will illustrate how operator
  888. precedence is handled.
  889.  
  890.    The source code for this calculator is named `rpcalc.y'.  The `.y'
  891. extension is a convention used for Bison input files.
  892.  
  893. * Menu:
  894.  
  895. * Decls: Rpcalc Decls.    Bison and C declarations for rpcalc.
  896. * Rules: Rpcalc Rules.    Grammar Rules for rpcalc, with explanation.
  897. * Input: Rpcalc Input.    Explaining the rules for `input'.
  898. * Line: Rpcalc Line.      Explaining the rules for `line'.
  899. * Expr: Rpcalc Expr.      Explaining the rules for `expr'.
  900. * Lexer: Rpcalc Lexer.    The lexical analyzer.
  901. * Main: Rpcalc Main.      The controlling function.
  902. * Error: Rpcalc Error.    The error reporting function.
  903. * Gen: Rpcalc Gen.        Running Bison on the grammar file.
  904. * Comp: Rpcalc Compile.   Run the C compiler on the output code.
  905.  
  906. 
  907. File: bison.info,  Node: Rpcalc Decls,  Next: Rpcalc Rules,  Prev: RPN calc,  Up: RPN calc
  908.  
  909. Declarations for `rpcalc'
  910. -------------------------
  911.  
  912.    Here are the C and Bison declarations for the reverse polish
  913. notation calculator.  As in C, comments are placed between `/*...*/'.
  914.  
  915.      /* Reverse polish notation calculator. */
  916.      
  917.      %{
  918.      #define YYSTYPE double
  919.      #include <math.h>
  920.      %}
  921.      
  922.      %token NUM
  923.      
  924.      %% /* Grammar rules and actions follow */
  925.  
  926.    The C declarations section (*note C Declarations::.) contains two
  927. preprocessor directives.
  928.  
  929.    The `#define' directive defines the macro `YYSTYPE', thus
  930. specifying the C data type for semantic values of both tokens and
  931. groupings (*note Value Type::.).  The Bison parser will use whatever
  932. type `YYSTYPE' is defined as; if you don't define it, `int' is the
  933. default.  Because we specify `double', each token and each expression
  934. has an associated value, which is a floating point number.
  935.  
  936.    The `#include' directive is used to declare the exponentiation
  937. function `pow'.
  938.  
  939.    The second section, Bison declarations, provides information to
  940. Bison about the token types (*note Bison Declarations::.).  Each
  941. terminal symbol that is not a single-character literal must be
  942. declared here.  (Single-character literals normally don't need to be
  943. declared.)  In this example, all the arithmetic operators are
  944. designated by single-character literals, so the only terminal symbol
  945. that needs to be declared is `NUM', the token type for numeric
  946. constants.
  947.  
  948. 
  949. File: bison.info,  Node: Rpcalc Rules,  Next: Rpcalc Input,  Prev: Rpcalc Decls,  Up: RPN Calc
  950.  
  951. Grammar Rules for `rpcalc'
  952. --------------------------
  953.  
  954.    Here are the grammar rules for the reverse polish notation
  955. calculator.
  956.  
  957.      input:    /* empty */
  958.              | input line
  959.      ;
  960.      
  961.      line:     '\n'
  962.              | exp '\n'  { printf ("\t%.10g\n", $1); }
  963.      ;
  964.      
  965.      exp:      NUM             { $$ = $1;         }
  966.              | exp exp '+'     { $$ = $1 + $2;    }
  967.              | exp exp '-'     { $$ = $1 - $2;    }
  968.              | exp exp '*'     { $$ = $1 * $2;    }
  969.              | exp exp '/'     { $$ = $1 / $2;    }
  970.            /* Exponentiation */
  971.              | exp exp '^'     { $$ = pow ($1, $2); }
  972.            /* Unary minus    */
  973.              | exp 'n'         { $$ = -$1;        }
  974.      ;
  975.      %%
  976.  
  977.    The groupings of the rpcalc "language" defined here are the
  978. expression (given the name `exp'), the line of input (`line'), and the
  979. complete input transcript (`input').  Each of these nonterminal
  980. symbols has several alternate rules, joined by the `|' punctuator
  981. which is read as "or".  The following sections explain what these rules
  982. mean.
  983.  
  984.    The semantics of the language is determined by the actions taken
  985. when a grouping is recognized.  The actions are the C code that
  986. appears inside braces.  *Note Actions::.
  987.  
  988.    You must specify these actions in C, but Bison provides the means
  989. for passing semantic values between the rules.  In each action, the
  990. pseudo-variable `$$' stands for the semantic value for the grouping
  991. that the rule is going to construct.  Assigning a value to `$$' is the
  992. main job of most actions.  The semantic values of the components of the
  993. rule are referred to as `$1', `$2', and so on.
  994.  
  995. 
  996. File: bison.info,  Node: Rpcalc Input,  Next: Rpcalc Line,  Prev: Rpcalc Rules,  Up: RPN Calc
  997.  
  998. Explanation of `input'
  999. ......................
  1000.  
  1001.    Consider the definition of `input':
  1002.  
  1003.      input:    /* empty */
  1004.              | input line
  1005.      ;
  1006.  
  1007.    This definition reads as follows: "A complete input is either an
  1008. empty string, or a complete input followed by an input line".  Notice
  1009. that "complete input" is defined in terms of itself.  This definition
  1010. is said to be "left recursive" since `input' appears always as the
  1011. leftmost symbol in the sequence.  *Note Recursion::.
  1012.  
  1013.    The first alternative is empty because there are no symbols between
  1014. the colon and the first `|'; this means that `input' can match an
  1015. empty string of input (no tokens).  We write the rules this way
  1016. because it is legitimate to type `Ctrl-d' right after you start the
  1017. calculator.  It's conventional to put an empty alternative first and
  1018. write the comment `/* empty */' in it.
  1019.  
  1020.    The second alternate rule (`input line') handles all nontrivial
  1021. input.  It means, "After reading any number of lines, read one more
  1022. line if possible."  The left recursion makes this rule into a loop. 
  1023. Since the first alternative matches empty input, the loop can be
  1024. executed zero or more times.
  1025.  
  1026.    The parser function `yyparse' continues to process input until a
  1027. grammatical error is seen or the lexical analyzer says there are no
  1028. more input tokens; we will arrange for the latter to happen at end of
  1029. file.
  1030.  
  1031. 
  1032. File: bison.info,  Node: Rpcalc Line,  Next: Rpcalc Expr,  Prev: Rpcalc Input,  Up: RPN Calc
  1033.  
  1034. Explanation of `line'
  1035. .....................
  1036.  
  1037.    Now consider the definition of `line':
  1038.  
  1039.      line:     '\n'
  1040.              | exp '\n'  { printf ("\t%.10g\n", $1); }
  1041.      ;
  1042.  
  1043.    The first alternative is a token which is a newline character; this
  1044. means that rpcalc accepts a blank line (and ignores it, since there is
  1045. no action).  The second alternative is an expression followed by a
  1046. newline.  This is the alternative that makes rpcalc useful.  The
  1047. semantic value of the `exp' grouping is the value of `$1' because the
  1048. `exp' in question is the first symbol in the alternative.  The action
  1049. prints this value, which is the result of the computation the user
  1050. asked for.
  1051.  
  1052.    This action is unusual because it does not assign a value to `$$'. 
  1053. As a consequence, the semantic value associated with the `line' is
  1054. uninitialized (its value will be unpredictable).  This would be a bug
  1055. if that value were ever used, but we don't use it: once rpcalc has
  1056. printed the value of the user's input line, that value is no longer
  1057. needed.
  1058.  
  1059. 
  1060. File: bison.info,  Node: Rpcalc Expr,  Next: Rpcalc Lexer,  Prev: Rpcalc Line,  Up: RPN Calc
  1061.  
  1062. Explanation of `expr'
  1063. .....................
  1064.  
  1065.    The `exp' grouping has several rules, one for each kind of
  1066. expression.  The first rule handles the simplest expressions: those
  1067. that are just numbers.  The second handles an addition-expression,
  1068. which looks like two expressions followed by a plus-sign.  The third
  1069. handles subtraction, and so on.
  1070.  
  1071.      exp:      NUM
  1072.              | exp exp '+'     { $$ = $1 + $2;    }
  1073.              | exp exp '-'     { $$ = $1 - $2;    }
  1074.              ...
  1075.              ;
  1076.  
  1077.    We have used `|' to join all the rules for `exp', but we could
  1078. equally well have written them separately:
  1079.  
  1080.      exp:      NUM ;
  1081.      exp:      exp exp '+'     { $$ = $1 + $2;    } ;
  1082.      exp:      exp exp '-'     { $$ = $1 - $2;    } ;
  1083.              ...
  1084.  
  1085.    Most of the rules have actions that compute the value of the
  1086. expression in terms of the value of its parts.  For example, in the
  1087. rule for addition, `$1' refers to the first component `exp' and `$2'
  1088. refers to the second one.  The third component, `'+'', has no
  1089. meaningful associated semantic value, but if it had one you could
  1090. refer to it as `$3'.  When `yyparse' recognizes a sum expression using
  1091. this rule, the sum of the two subexpressions' values is produced as
  1092. the value of the entire expression.  *Note Actions::.
  1093.  
  1094.    You don't have to give an action for every rule.  When a rule has no
  1095. action, Bison by default copies the value of `$1' into `$$'.  This is
  1096. what happens in the first rule (the one that uses `NUM').
  1097.  
  1098.    The formatting shown here is the recommended convention, but Bison
  1099. does not require it.  You can add or change whitespace as much as you
  1100. wish.  For example, this:
  1101.  
  1102.      exp   : NUM | exp exp '+' {$$ = $1 + $2; } | ...
  1103.  
  1104. means the same thing as this:
  1105.  
  1106.      exp:      NUM
  1107.              | exp exp '+'    { $$ = $1 + $2; }
  1108.              | ...
  1109.  
  1110. The latter, however, is much more readable.
  1111.  
  1112.